//
//  NSDate+Extend.m
//  CoreCategory
//
//  Created by 成林 on 15/4/6.
//  Copyright (c) 2015年 沐汐. All rights reserved.
//

#import "NSDate+MJUI.h"

static const NSCalendarUnit calendarUnit = (NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekOfMonth |  NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond | NSCalendarUnitWeekday | NSCalendarUnitWeekdayOrdinal);

@interface NSDate ()


@end

@implementation NSDate (MJUI)
/**
 时间戳装换为指定格式
 
 @param formatter 转换格式 @"yyyy-MM-dd HH:mm:ss zzz"]; aaa 上午/下午
 @return 字符串
 */
- (NSString *)mj_dateConvertToStringByFormat:(NSString *)formatter{
    
    NSString *strTime;
    
    NSDateFormatter *dateformatter=[NSDateFormatter new];//定义时间格式
    dateformatter.locale = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"];

    [dateformatter setDateFormat:formatter];
    
    dateformatter.AMSymbol = @"上午";
    
    dateformatter.PMSymbol = @"下午";
    
    strTime =[dateformatter stringFromDate:self];//转为字符串
    
    return strTime;
}

/*
 *  时间戳
 */
-(NSString *)mj_timestampStr{

    
    NSTimeInterval timeInterval = [self timeIntervalSince1970] ;
    NSString *timeString = [NSString stringWithFormat:@"%.0f",timeInterval];
    
    return [timeString copy];
}

- (NSInteger)mj_dateConvertToAge;
{
    // 出生日期转换 年月日
    NSDateComponents *components1 = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:self];
    NSInteger brithDateYear  = [components1 year];
    NSInteger brithDateDay   = [components1 day];
    NSInteger brithDateMonth = [components1 month];
    
    // 获取系统当前 年月日
    NSDateComponents *components2 = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:[NSDate date]];
    NSInteger currentDateYear  = [components2 year];
    NSInteger currentDateDay   = [components2 day];
    NSInteger currentDateMonth = [components2 month];
    
    // 计算年龄
    NSInteger iAge = currentDateYear - brithDateYear - 1;
    if ((currentDateMonth > brithDateMonth) || (currentDateMonth == brithDateMonth && currentDateDay >= brithDateDay)) {
        iAge++;
    }
    
    return iAge;
}

- (NSString *)mj_compareCurrentTime
{
    
    //得到与当前时间差
    NSTimeInterval time = fabs([[NSDate date] timeIntervalSinceDate:self]);
    
    NSString *returnString = @"";
    if(time < 60)
        returnString = @"刚刚";
    else if(time >=60 && time < 3600)
        returnString = [NSString stringWithFormat:@"%.0f分钟前",time/60];
    else if(time >= 3600 && time < 3600 * 24)
        returnString = [NSString stringWithFormat:@"%.0f小时前",time/(60 * 60)];
    else if(time >= 3600 * 24 && time < 3600 * 24 * 30)
        returnString = [NSString stringWithFormat:@"%.0f天前",time/(60 * 60 * 24)];
    else if(time >= 3600 * 24 * 30 && time < 3600 * 24 * 30 * 12)
        returnString = [NSString stringWithFormat:@"%.0f月前",time/(60 * 60 * 24 * 30)];
    else if(time >= 3600 * 24 * 30 * 12)
        returnString = [NSString stringWithFormat:@"%.0f年前",time/(60 * 60 * 24 * 30 * 12)];
    
    return returnString;
}
+ (NSInteger)mj_differenceDayWithStartDate:(NSDate *)startDate endDate:(NSDate *)endDate {
    //获得当前时间
   NSDate *now = endDate;
   NSDate *oldDate = startDate;
   NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
   unsigned int unitFlags = NSCalendarUnitDay;
   NSDateComponents *comps = [gregorian components:unitFlags fromDate:oldDate toDate:now  options:0];
    NSInteger dayDifference = [comps day];
    return dayDifference;
  
}

/**
 相差时间
 */
+ (NSDateComponents *)mj_differenceTimeWithStartDate:(NSDate *)startDate endDate:(NSDate *)endDate {
    //获得当前时间
   NSDate *now = endDate;
   NSDate *oldDate = startDate;
   NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
   unsigned int unitFlags = NSCalendarUnitYear| NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond ;
   NSDateComponents *comps = [gregorian components:unitFlags fromDate:oldDate toDate:now  options:0];

    return comps;
}

- (NSString *)mj_differenceCurrentTime {
    //获得当前时间
   NSDate *now = [NSDate date];
   NSDate *oldDate = self;
   NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
   unsigned int unitFlags = NSCalendarUnitDay;
   NSDateComponents *comps = [gregorian components:unitFlags fromDate:oldDate toDate:now  options:0];
    NSInteger dayDifference = [comps day];
    
    if(dayDifference > 30){
        return [oldDate mj_dateConvertToStringByFormat:@"yyyy/MM/dd"];
    }else if(dayDifference > 1){
        return [NSString stringWithFormat:@"%ld天前",dayDifference];
    }else if(dayDifference == 1){
        return @"昨天";
    }else if(dayDifference == 0){
        return @"今天";
    }
    return @"今天";

}

- (BOOL)mj_isToday {
    //获得当前时间
    NSDate *date = self;
    BOOL isToday = [[NSCalendar currentCalendar] isDateInToday:date];
    return isToday;

}

#pragma mark - 获取指定日期的星期
- (NSInteger)mj_weekday {
    NSCalendar  *calendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *components = [calendar components:calendarUnit fromDate:self];
    return components.weekday;
}

#pragma mark - 获取指定日期的星期
- (NSString *)mj_weekdayString {
    switch (self.mj_weekday - 1) {
        case 0:
        {
            return @"星期日";
        }
            break;
        case 1:
        {
            return @"星期一";
        }
            break;
        case 2:
        {
            return @"星期二";
        }
            break;
        case 3:
        {
            return @"星期三";
        }
            break;
        case 4:
        {
            return @"星期四";
        }
            break;
        case 5:
        {
            return @"星期五";
        }
            break;
        case 6:
        {
            return @"星期六";
        }
            break;
            
        default:
            break;
    }
    
    return @"";
}

/**
 *  时间算法
 *
 *  @author Aron
 *  @date 2016-01-06
 *
 *  @param year  计算年=》加1年：1；减1年：-1
 *  @param month 计算月=》加1月：1；减1月：-1
 *  @param day   计算天=》加一周：7；减一周：-7
 *
 *  @return 计算好的时间
 */
-(NSDate *)mj_expectDateWithYear:(int)year month:(int)month day:(int)day{
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    //NSCalendarIdentifierGregorian:iOS8之前用NSGregorianCalendar
    NSDateComponents *comps = nil;
    
    comps = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:self];
    //NSCalendarUnitYear:iOS8之前用NSYearCalendarUnit,NSCalendarUnitMonth,NSCalendarUnitDay同理
    
    NSDateComponents *adcomps = [[NSDateComponents alloc] init];
    
    [adcomps setYear:year];
    
    [adcomps setMonth:month];
    
    [adcomps setDay:day];
    
    return [calendar dateByAddingComponents:adcomps toDate:self options:0];
}

//获取一周时间 数组
+ (NSMutableArray *)getCurrentWeeksWithFirstDiff:(NSInteger)first lastDiff:(NSInteger)last{
    
    NSMutableArray *eightArr = [[NSMutableArray alloc] init];
    for (NSInteger i = first; i < last + 1; i ++) {
        //从现在开始的24小时
        NSTimeInterval secondsPerDay = i * 24*60*60;
        NSDate *curDate = [NSDate dateWithTimeIntervalSinceNow:secondsPerDay];
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        dateFormatter.locale = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"];
        [dateFormatter setDateFormat:@"yyyy-MM-dd"];
        NSString *dateStr = [dateFormatter stringFromDate:curDate];//几月几号
        //组合时间
        NSString *strTime = [NSString stringWithFormat:@"%@",dateStr];
        [eightArr addObject:strTime];
    }
    return eightArr;
}
@end
